// James Fancy, May 2009
// class AlReader to read text files
function AlFsoReader(filename) {
    // constructor
    this.$filename = filename;
    this.$fso = new ActiveXObject("Scripting.FileSystemObject");

    this.$istream = null;

    // functions
    this.open = function(filename) {
        if (typeof(filename) == "string") {
            this.$filename = filename;
        }

        if (typeof(this.$filename) == "undefined") {
            throw new Error(0, "expect a valid filename");
        }

        try {
            this.$istream = this.$fso.OpenTextFile(this.$filename);
            return true;
        } catch (err) {
            this.$istream = null;
            return false;
        }
    }

    this.isValid = function() {
        return this.$istream != null;
    }

    this.readAll = function() {
        if (this.$istream == null) {
            return null;
        }

        return this.$istream.ReadAll();
    }

    this.readLine = function() {
        return this.$istream.AtEndOfStream ? null : this.$istream.ReadLine();
    }

    this.readLines = function() {
        if (this.$istream == null) {
            return null;
        }

        var lines = new Array();
        while (!this.$istream.AtEndOfStream) {
            lines.push(this.readLine());
        }
    }

    this.close = function() {
        if (this.$istream != null) {
            this.$istream.Close();
        }
    }

    // initialize
    this.open();
}

// class AlFsoWriter to write text file
function AlFsoWriter(filename) {
    this.$filename = filename;
    this.$fso = new ActiveXObject("Scripting.FileSystemObject");

    this.$ostream = null;

    // functions
    this.open = function(filename) {
        if (typeof(filename) == "string") {
            this.$filename = filename;
        }

        if (typeof(this.$filename) == "undefined") {
            throw new Error(0, "expect a valid filename");
        }

        try {
            this.$ostream = this.$fso.OpenTextFile(this.$filename, 2, true);
            return true;
        } catch (err) {
            this.$ostream = null;
            return false;
        }
    }

    this.isValid = function() {
        return this.$ostream != null;
    }

    this.writeLine = function(line) {
        if (this.$ostream == null) {
            return;
        }
        this.$ostream.WriteLine(line);
    }

    this.close = function() {
        if (this.$ostream != null) {
            this.$ostream.Close();
        }
    }

    // initialize
    this.open();
}
